package com.neuedu.controller;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.DongShu;
import com.neuedu.model.FangChan;
import com.neuedu.model.RenYuan;
import com.neuedu.model.XiaoQu;
import com.neuedu.service.RenYuanService;
import com.neuedu.service.impl.RenYuanServiceImpl;
import com.neuedu.util.R;
import com.neuedu.vo.PageVO;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.Map;
import java.util.UUID;

public class RenYuanController {
    public static final String FILE_PATH="D:\\teach\\web项目\\exercise20240318物业管理系统\\src\\com\\neuedu\\wuye";

    RenYuanService renYuanService =new RenYuanServiceImpl();
    public R page(JSONObject jsonObject){
        Integer pageNo=jsonObject.getInteger("pageNo");
        Integer pageSize=jsonObject.getInteger("pageSize");
        //在某两个日期之间，like 一个给定的小区名称。
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");

        PageVO<RenYuan> pageVO = renYuanService.selectOnePage(pageNo, pageSize, startTime, endTime, name);

        return R.ok().setData(pageVO);
    }

    public void showImage(HttpServletRequest request, HttpServletResponse response){
        String fileName = request.getParameter("fileName");
        File file=new File(FILE_PATH+File.separator+fileName);
        try {
            BufferedImage read = ImageIO.read(file);
            ImageIO.write(read,"png",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public R uploadIcon(JSONObject jsonObject){
        //需要接收两个参数
        //1、你是谁
        String base64=jsonObject.getString("base64");

        //1、base64还原为图片，保存至服务器的硬盘
        byte[] bytes = Base64.getDecoder().decode(base64);
        //写文件，文件名应该叫啥？
        String fileName= UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
        fileName=fileName+".jpg";
        FileOutputStream fos=null;
        try {
            fos =new FileOutputStream(new File(FILE_PATH+File.separator+fileName));
            fos.write(bytes);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return R.ok().setData(fileName);
    }


    public R add(JSONObject jsonObject){
        String suoshuxiaoqu = jsonObject.getString("suoshuxiaoqu");
        String fangchan = jsonObject.getString("fangchan");
        String mingcheng = jsonObject.getString("mingcheng");
        String shenfenzhenghao = jsonObject.getString("shenfenzhenghao");
        String chushengriqi = jsonObject.getString("chushengriqi");
        Integer xingbie = jsonObject.getInteger("xingbie");
        Integer chengyuanleixing = jsonObject.getInteger("chengyuanleixing");
        String icon = jsonObject.getString("icon");
        String lianxifangshi = jsonObject.getString("lianxifangshi");
        String miaoshu = jsonObject.getString("miaoshu");
        String zhiye=jsonObject.getString("zhiye");


        RenYuan renYuan=new RenYuan();
        renYuan.setSuoshuxiaoqu(suoshuxiaoqu);
        renYuan.setFangchan(fangchan);
        renYuan.setMingcheng(mingcheng);
        renYuan.setShenfenzhenghao(shenfenzhenghao);
        renYuan.setChushengriqi(chushengriqi);
        renYuan.setXingbie(xingbie);
        renYuan.setChengyuanleixing(chengyuanleixing);
        renYuan.setIcon(icon);
        renYuan.setLianxifangshi(lianxifangshi);
        renYuan.setMiaoshu(miaoshu);
        renYuan.setZhiye(zhiye);

        //调用service，录入一个新的小区
        int result = renYuanService.add(renYuan);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }

    }

    public R showRenYuan(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        RenYuan renYuan = renYuanService.findRenYuanById(id);
        return R.ok().setData(renYuan);
    }

    public R showImg(Map<String,String[]> map){
        String fileName = map.get("fileName")[0];
        File file=new File(FILE_PATH+ File.separator + fileName);
        FileInputStream fis=null;
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        try {
            fis=new FileInputStream(file);
            byte [] bytes=new byte [1024*8];
            int len=0;
            while ((len=fis.read(bytes))!=-1){
                baos.write(bytes,0,len);
            }
            byte[] imageByte = baos.toByteArray();
            String base64 = Base64.getEncoder().encodeToString(imageByte);
            return R.ok().setData(base64);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("图片展示失败");
    }

    public R edit(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        String suoshuxiaoqu = jsonObject.getString("suoshuxiaoqu");
        String fangchan = jsonObject.getString("fangchan");
        String mingcheng = jsonObject.getString("mingcheng");
        String shenfenzhenghao = jsonObject.getString("shenfenzhenghao");
        String chushengriqi = jsonObject.getString("chushengriqi");
        Integer xingbie = jsonObject.getInteger("xingbie");
        Integer chengyuanleixing = jsonObject.getInteger("chengyuanleixing");
        String icon = jsonObject.getString("icon");
        String lianxifangshi = jsonObject.getString("lianxifangshi");
        String miaoshu = jsonObject.getString("miaoshu");
        String zhiye=jsonObject.getString("zhiye");

        RenYuan renYuan = renYuanService.findRenYuanById(id);
        renYuan.setSuoshuxiaoqu(suoshuxiaoqu);
        renYuan.setFangchan(fangchan);
        renYuan.setMingcheng(mingcheng);
        renYuan.setShenfenzhenghao(shenfenzhenghao);
        renYuan.setChushengriqi(chushengriqi);
        renYuan.setXingbie(xingbie);
        renYuan.setChengyuanleixing(chengyuanleixing);
        renYuan.setIcon(icon);
        renYuan.setLianxifangshi(lianxifangshi);
        renYuan.setMiaoshu(miaoshu);
        renYuan.setZhiye(zhiye);

        //调用service，录入一个新的小区
        int result = renYuanService.editRenYuanInfo(renYuan);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }

    }
}
